﻿// My methods here.
//-----------------------------------------------------------------------
// <Copyright file=LoaiKhuyenMaiAndKhuyenMai company=DHPD>
// Copyright (c) DHPD Corporation (http://dhpd.edu.vn). All rights reserved.
// </copyright>
// <author>HP</author>
//-----------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using KM.DAT;
using KM.ENT;
using KM.Models;

namespace KM.DAT
{
/// <summary>
/// This is datLoaiKhuyenMaiAndKhuyenMai class
/// </summary>

public class datLoaiKhuyenMaiAndKhuyenMai
{
    public datLoaiKhuyenMaiAndKhuyenMai()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    SqlCommand cmd;
    SqlDataReader dr;
    string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
    #region  === tạo mới và sửa LoaiKhuyenMaiAndKhuyenMai===
    public void createEdit(entLoaiKhuyenMaiAndKhuyenMai entLKMAKM)
    {
        SqlConnection sqlConnection = new SqlConnection(constr);
        sqlConnection.Open();
        cmd = new SqlCommand("sp_LoaiKhuyenMaiAndKhuyenMaiCreateEdit", sqlConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("Id", entLKMAKM.Id);
		cmd.Parameters.AddWithValue("KhuyenMaiId", entLKMAKM.KhuyenMaiId);
		cmd.Parameters.AddWithValue("LoaiKhuyenMaiId", entLKMAKM.LoaiKhuyenMaiId);
		
        cmd.ExecuteNonQuery();
        sqlConnection.Close();
    }
    #endregion
    
    #region  === getall LoaiKhuyenMaiAndKhuyenMai===
    public List<entLoaiKhuyenMaiAndKhuyenMai> getAll( ref PagingInfoModel page)
    {
        SqlConnection sqlConnection = new SqlConnection(constr);
        sqlConnection.Open();
        cmd = new SqlCommand("sp_LoaiKhuyenMaiAndKhuyenMaiGetAll", sqlConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@PageIndex", page.CurrentPage);
        cmd.Parameters.AddWithValue("@PageSize", page.recordPerPage);
        cmd.Parameters.AddWithValue("@RecordCount", page.totalRecords).Direction = ParameterDirection.Output;
        dr = cmd.ExecuteReader();
        entLoaiKhuyenMaiAndKhuyenMai entLKMAKM = new entLoaiKhuyenMaiAndKhuyenMai();
        List<entLoaiKhuyenMaiAndKhuyenMai> lst = new List<entLoaiKhuyenMaiAndKhuyenMai>();
        while (dr.Read())
        {
            entLKMAKM = new entLoaiKhuyenMaiAndKhuyenMai();
            entLKMAKM.Id = Convert.ToInt32(dr["Id"].ToString());
			entLKMAKM.KhuyenMaiId = Convert.ToInt32(dr["KhuyenMaiId"].ToString());
			entLKMAKM.LoaiKhuyenMaiId = Convert.ToInt32(dr["LoaiKhuyenMaiId"].ToString());
			
            lst.Add(entLKMAKM);
        }
        
        dr.Close();
        if (lst.Count > 0)
        {
            // int.Parse(cmd.Parameters["@RecordCount"].Value.ToString());
            page.totalRecords = int.Parse(cmd.Parameters["@RecordCount"].Value.ToString());
        }
        
        sqlConnection.Close();
        return lst;
    }
    #endregion
    #region  === getall LoaiKhuyenMaiAndKhuyenMai===
    public List<entLoaiKhuyenMaiAndKhuyenMai> getAllByForeignKey(int Id, ref PagingInfoModel page)
    {
        SqlConnection sqlConnection = new SqlConnection(constr);
        sqlConnection.Open();
        cmd = new SqlCommand("sp_LoaiKhuyenMaiAndKhuyenMaiGetAllByForeignKey", sqlConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("Id", Id);
        cmd.Parameters.AddWithValue("@PageIndex", page.CurrentPage);
        cmd.Parameters.AddWithValue("@PageSize", page.recordPerPage);
        cmd.Parameters.AddWithValue("@RecordCount", page.totalRecords).Direction = ParameterDirection.Output;
        dr = cmd.ExecuteReader();
        entLoaiKhuyenMaiAndKhuyenMai entLKMAKM = new entLoaiKhuyenMaiAndKhuyenMai();
        List<entLoaiKhuyenMaiAndKhuyenMai> lst = new List<entLoaiKhuyenMaiAndKhuyenMai>();
        while (dr.Read())
        {
            entLKMAKM = new entLoaiKhuyenMaiAndKhuyenMai();
            entLKMAKM.Id = Convert.ToInt32(dr["Id"].ToString());
			entLKMAKM.KhuyenMaiId = Convert.ToInt32(dr["KhuyenMaiId"].ToString());
			entLKMAKM.LoaiKhuyenMaiId = Convert.ToInt32(dr["LoaiKhuyenMaiId"].ToString());
			
            lst.Add(entLKMAKM);
        }
        
        dr.Close();
        if (lst.Count > 0)
        {
            // int.Parse(cmd.Parameters["@RecordCount"].Value.ToString());
            page.totalRecords = int.Parse(cmd.Parameters["@RecordCount"].Value.ToString());
        }
        
        sqlConnection.Close();
        return lst;
    }
    #endregion
    
    #region  === getinfo LoaiKhuyenMaiAndKhuyenMai===
    public entLoaiKhuyenMaiAndKhuyenMai getInfo(int Id)
    {
        SqlConnection sqlConnection = new SqlConnection(constr);
        sqlConnection.Open();
        cmd = new SqlCommand("sp_LoaiKhuyenMaiAndKhuyenMaiGetInfo", sqlConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter[] sqla = new SqlParameter[1];
        sqla[0] = new SqlParameter("Id", DbType.Int32);
        sqla[0].Value = Id;

        cmd.Parameters.AddRange(sqla);
        dr = cmd.ExecuteReader();
        entLoaiKhuyenMaiAndKhuyenMai entLKMAKM = new entLoaiKhuyenMaiAndKhuyenMai();
        if (dr.Read())
        {
            entLKMAKM.Id = Convert.ToInt32(dr["Id"].ToString());
			entLKMAKM.KhuyenMaiId = Convert.ToInt32(dr["KhuyenMaiId"].ToString());
			entLKMAKM.LoaiKhuyenMaiId = Convert.ToInt32(dr["LoaiKhuyenMaiId"].ToString());
			
        }
        dr.Close();
        sqlConnection.Close();
        return entLKMAKM;
    }
    #endregion
    
    #region === xoa LoaiKhuyenMaiAndKhuyenMai===
    public void delete(int id)
    {
        SqlConnection sqlConnection = new SqlConnection(constr);
        sqlConnection.Open();
        cmd = new SqlCommand("sp_LoaiKhuyenMaiAndKhuyenMaiDeleteById", sqlConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter[] sqla = new SqlParameter[1];
        sqla[0] = new SqlParameter("Id", DbType.Int32);
        sqla[0].Value = id;

        cmd.Parameters.AddRange(sqla);
        cmd.ExecuteNonQuery();
        sqlConnection.Close();
    }
    #endregion
}
}
